import request from '@/config/axios'

export interface BookVO {
  id?: number
  title: string
  subtitle?: string
  author: string
  publisher?: string
  isbn?: string
  categoryId: number
  coverUrl: string
  description?: string
  pageCount: number
  wordCount?: number
  language?: string
  level?: string
  type?: string
  difficultyScore?: number
  ageRange?: string
  status?: number
  isFree?: number
  sort?: number
  readCount?: number
  createTime?: string
  updateTime?: string
  creator?: string
  updater?: string
  deleted?: boolean
  tenantId?: number
}

export interface BookCategoryVO {
  id?: number
  name: string
  description?: string
  bookCount?: number
  createTime?: string
}

// 查询绘本列表
export const getBookPage = (params: {
  pageNo?: number
  pageSize?: number
  keyword?: string
  categoryId?: number
  status?: number
}) => {
  // 转换参数名以匹配后端接口
  const backendParams = {
    current: params.pageNo || 1,
    size: params.pageSize || 10,
    keyword: params.keyword,
    categoryId: params.categoryId,
    status: params.status
  }
  return request.get({ url: '/api/admin/books', params: backendParams })
}

// 创建绘本
export const createBook = (data: BookVO) => {
  return request.post({ url: '/api/admin/books', data })
}

// 更新绘本
export const updateBook = (id: number, data: BookVO) => {
  return request.put({ url: `/api/admin/books/${id}`, data })
}

// 获取绘本详情
export const getBook = (id: number) => {
  return request.get({ url: `/api/admin/books/${id}` })
}

// 删除绘本
export const deleteBook = (id: number) => {
  return request.delete({ url: `/api/admin/books/${id}` })
}

// 查询绘本页数
export const getBookPages = (id: number) => {
  return request.get({ url: `/api/admin/books/${id}/pages` })
}

// 获取绘本分类列表
export const getBookCategories = () => {
  return request.get({ url: '/api/admin/books/categories' })
}

// 创建绘本分类
export const createBookCategory = (data: BookCategoryVO) => {
  return request.post({ url: '/api/admin/books/categories', data })
}

// 更新绘本分类
export const updateBookCategory = (id: number, data: BookCategoryVO) => {
  return request.put({ url: `/api/admin/books/categories/${id}`, data })
}

// 删除绘本分类
export const deleteBookCategory = (id: number) => {
  return request.delete({ url: `/api/admin/books/categories/${id}` })
}

// 批量删除绘本
export const deleteBookBatch = (ids: number[]) => {
  return request.delete({ url: '/api/admin/books/batch', params: { ids: ids.join(',') } })
}